package com.busionline.wotu.woochenandroid.model;

import com.busionline.wotu.woochenandroid.bean.ArticleBean;
import com.busionline.wotu.woochenandroid.bean.BannerBean;
import com.busionline.wotu.woochenandroid.bean.BaseBean;
import com.busionline.wotu.woochenandroid.bean.PageListDataBean;
import com.busionline.wotu.woochenandroid.http.RetrofitUtil;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by admin on 2018/5/15.
 */

public class HomeModel {
    private static final String TAG = "HomeModel";

    public interface CallBack<T>{
        void onSuccess(List<T> data);
        void fail(Throwable t);
    }

    /**
     * 首页轮播
     * @param callBack
     */
    public void getHomeBanner(final CallBack callBack) {
        Call<BaseBean<List<BannerBean>>> call = RetrofitUtil.getApi()
                .main_banner();
        call.enqueue(new Callback<BaseBean<List<BannerBean>>>() {
            @Override
            public void onResponse(Call<BaseBean<List<BannerBean>>> call, Response<BaseBean<List<BannerBean>>> response) {
                List<BannerBean> data = response.body().data;
                callBack.onSuccess(data);
            }

            @Override
            public void onFailure(Call<BaseBean<List<BannerBean>>> call, Throwable t) {
                callBack.fail(t);
            }
        });
    }

    /**
     * 首页列表数据
     * @param page
     * @param callBack
     */
    public void getHomeData(int page,final CallBack callBack) {
        Call<BaseBean<PageListDataBean<ArticleBean>>> call = RetrofitUtil.getApi()
                .main_list(page);
        call.enqueue(new Callback<BaseBean<PageListDataBean<ArticleBean>>>() {
            @Override
            public void onResponse(Call<BaseBean<PageListDataBean<ArticleBean>>> call, Response<BaseBean<PageListDataBean<ArticleBean>>> response) {
                List<ArticleBean> datas = response.body().data.getDatas();
                callBack.onSuccess(datas);
            }

            @Override
            public void onFailure(Call<BaseBean<PageListDataBean<ArticleBean>>> call, Throwable t) {
                callBack.fail(t);
            }
        });
    }

}
